package com.example.demo421.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Base64;

/**
 * Author zss
 * 2022/4/21 3:42 下午
 * com.example.basicsecret.controller
 * SController
 * describe：
 **/
@Slf4j
@RequestMapping("/test")
@RestController
public class MController {
    Logger logger = LoggerFactory.getLogger(MController.class);

    @Autowired
    private RestTemplate laxRrestTemplate;
    @Autowired
    private String getStr;
    @RequestMapping("/test")
    public void test() {
        System.out.println(getStr);
    }

    @GetMapping("/basic")
    public void basicAuth() {
        // 客户 API Key
        final String customerAPIKey = "输入有效的APIKey";
        // 客户 API Secret
        final String customerAPISecret = "输入有效的APISecret";

        // 拼接客户 ID 和客户鉴权信息并使用 base64 编码
        String plainCredentials =  customerAPIKey + ":" + customerAPISecret;
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;

        String urlString = "https://sms.api.juphoon.com/sms/v1";
        String content = "{\n" +
                "            \"templateId\":\"T1ede22773\",\n" +
                "        \"templateParams\":[\n" +
                "           \"121234\"], \n" +
                "      \"to\":\"+13137992197\",\n" +
                "      \"bizId\":\"123\"\n" +
                "      }";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("X-Jcc-Authorization", authorizationHeader);
        HttpEntity requestEntity = new HttpEntity<>(content, headers);
        ResponseEntity<String> response = laxRrestTemplate.exchange(urlString, HttpMethod.POST, requestEntity, String.class);
        logger.info("code {}",response.getStatusCode().value());
        logger.info("body {}",response.getBody());
        }
}
